package ltd.newbee.mall.controller.admin;

import ltd.newbee.mall.common.NewBeeMallCategoryLevelEnum;
import ltd.newbee.mall.entity.GoodsCategory;
import ltd.newbee.mall.service.NewBeeMallCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class ItemController {

    @Autowired
    private NewBeeMallCategoryService newBeeMallCategoryService;
    /*url:http://localhost:28089/admin/goods/edit
    *
    * */
    @RequestMapping("/goods/edit")
    public String items(HttpServletRequest request){
        request.setAttribute("path", "edit");
        List<GoodsCategory> firstLevelCategories=newBeeMallCategoryService.selectByLevelAndParentIdsAndNumber(Collections.singletonList(0L), NewBeeMallCategoryLevelEnum.LEVEL_ONE.getLevel());
        request.setAttribute("firstLevelCategories",firstLevelCategories);
        if (!CollectionUtils.isEmpty(firstLevelCategories)){
            List<GoodsCategory> secondLevelCategories=newBeeMallCategoryService.selectByLevelAndParentIdsAndNumber(Collections.singletonList(firstLevelCategories.get(0).getCategoryId()),NewBeeMallCategoryLevelEnum.LEVEL_TWO.getLevel());
            if(!CollectionUtils.isEmpty(secondLevelCategories)){
                List<GoodsCategory> thirdLevelCategories=newBeeMallCategoryService.selectByLevelAndParentIdsAndNumber(Collections.singletonList(secondLevelCategories.get(0).getCategoryId()),NewBeeMallCategoryLevelEnum.LEVEL_THREE.getLevel());
                request.setAttribute("firstLevelCategories",firstLevelCategories);
                request.setAttribute("secondLevelCategories",secondLevelCategories);
                request.setAttribute("thirdLevelCategories",thirdLevelCategories);
                return "admin/newbee_mall_goods_edit";
            }
        }
        return "error/error_5xx";
    }

}
